package qe.spider.grab;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.UnicodeUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import qe.spider.vo.StockList;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;


//看盘
@Slf4j
public class GrabKanPan {
    private String userID;
    private String token;

    public GrabKanPan(String userID, String token) {
        this.userID = userID;
        this.token = token;
    }

    public static void main(String[] args) throws IOException {
        new GrabKanPan("111775r", "13f1764f728c8db11f85b98e524bec3c").grab();
    }


    public StockList grab() throws IOException {
        return grab(new Date());
    }

    public StockList grab(Date date) throws IOException {
        FormBody formBody = new FormBody.Builder()
                .add("c", "YiDongKanPan")
                .add("a", "KanPanNew")
                .add("Type", "1")
                .add("Order", "9")
                .add("Index", "0")
                .add("Date", DateUtil.format(date, "yyyyMMdd"))
                .add("st", "50")
                .add("UserID", userID)
                .add("Token", token)
                .build();

        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("https://pchq.kaipanla.com/w1/api/index.php")
//                .header("Accept-Encoding", "gzip, deflate, br")
                .header("Accept-Encoding", "br")
                .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                .header("Connection", "keep-alive")
                .header("Content-Length", "108")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Host", "pchq.kaipanla.com")
                .header("Origin", "https://www.kaipanla.com")
                .header("Referer", "https://www.kaipanla.com/")
                .header("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Microsoft Edge\";v=\"109\", \"Chromium\";v=\"109\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-site")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.140")
                .post(formBody)
                .build();
        Gson gson = new Gson();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);


            String result = UnicodeUtil.toString(response.body().string());
            log.info(result);
            StockList list = gson.fromJson(result, StockList.class);
            list.grabTime();
            log.debug(list.toString());
            return list;
        }

    }


}
